package com.s64.ch07.service.impl;

import com.s64.ch07.entity.SysUser;
import com.s64.ch07.mapper.SysUserMapper;
import com.s64.ch07.service.IScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Set;

@Service
public class ScoreServiceImpl implements IScoreService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    final String key = "s63:user:score";
    @Override
    public void incrby(Serializable id) {
        SysUser user = sysUserMapper.selectById(id);
        redisTemplate.opsForZSet().incrementScore(key, user, 1.0);
    }

    @Override
    public Set list(int range) {
        Set<ZSetOperations<String, SysUser>> set = redisTemplate.opsForZSet().reverseRangeWithScores(key, 0, range);
        return set;
    }
}
